
import sys
from PySide6 import QtCore,QtGui,QtWidgets
from PySide6.QtWidgets import QApplication,QMainWindow,QPushButton
from tkinter import *
from tkinter import filedialog

import pygame

class Music:
    def __init__(self,windown):
        self.windown=windown
        self.windown.title("只录音乐")
        self.windown.geometry("500x400")
        
        self.play_btn=Button(windown,text="播放音乐",command=self.play_music)
        self.play_btn.place(x=60,y=20)
        self.stop_btn=Button(windown,text="停止播放",command=self.stop_music)
        self.stop_btn.place(x=180,y=20)
        self.music_list_box=Listbox(windown)
        self.music_list_box.place(x=160,y=120)
        self.open_file_btn=Button(windown,text="本地音乐",command=self.add_to_playlist)
        self.open_file_btn.place(x=300,y=20)
    def add_to_playlist(self):
        file_path=filedialog.askopenfilename(title='选择音乐文件夹',filetypes=(('mp3')))
        if file_path:
            self.music_list_box.insert(END,file_path)
    def play_music(self):
        if len(self.music_list_box.curselection())==0:
            return
        index=int(self.music_list_box.curselection()[0])
        music_file=self.music_list_box.get(index)
        
        pygame.mixer.init()
        pygame.mixer.music.load(music_file)
        pygame.mixer.music.play()
        
    def pause_music(self):
        pygame.mixer.music.pause()
        
    def stop_music(self):
        pygame.mixer.music.stop()
        
if __name__=='__main__':
    windown=Tk()
    music_player=Music(windown)
    windown.mainloop()
    
        